<?php
namespace app\admin\controller;
use app\BaseController;
use think\facade\Db;
use think\Response;
class Base extends BaseController
{
    /*定义一个初始数组*/
    protected $info = array(
        'code'=>200,
        'msg'=>'提示',
        'total'=>0,//数据数量
        'data'=>[]
    );

    protected $adminData = array();
    
    protected function initialize(){
        if( session('?adminData')){

            //不受权限控制的页面
            $noPowerArr = [
                'index/index',
                'index/home',
                'user/password',
            ];


            //已登录
            $this->adminData = unserialize(session('adminData'));

            //判断是否管理员
            // if($this->adminData['role_id']!=1){
            if(false){
                $url = str_replace("/admin/","",strtolower(request() -> baseUrl()));//获取正在访问的
                $url = str_replace('.html','',$url);
                //判断访问的url是否在不受权限控制的列表中
                if(!in_array($url,$noPowerArr)){
                    $power =  Db::table('tb_admin_power')->where(['url'=>$url])->find();//查找访问的连接是否在权限表
                    
                    if($power){
                        $role =  Db::table('tb_admin_role')->where('id',$this->adminData['role_id'])->find();
                        if($role['power_id']){
                            $power_id_arr = explode(",",$role['power_id']);
                            if(!in_array($power['id'],$power_id_arr)){
                                $this->is_jqueryAjax();  
                            }
                        }else{
                            $this->is_jqueryAjax();  
                        }
                    }else{
                        $this->is_jqueryAjax();    
                    }
                }
            }
            // dump($this->adminData);
            $this->assign('is_super',$this->adminData['id']==1?1:0);
            $this->assign('adminData',$this->adminData);
            $config = [];
            // $config = Db::table('tb_config')->order('sort', 'dsc')->select()->toArray();
            $this->assign('config',$config);
        }else{
            //未登录
            exit('<script language="javascript">top.location.href="/admin/login/index"</script>');
            
        }
    }

    // 判断是否为 ajax 请求
    protected function is_jqueryAjax(){
        if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest"){ 
            // ajax 请求的处理方式 
            $this->info = ['code'=>0,'msg'=>'没有权限'];
            exit(json_encode($this->info));
        }else{ 
            // 正常请求的处理方式 
            header('location:/admin/login/cuowu');
        };
    }

    /**
     *  获取客户端IP地址
     */
    protected function get_ip()
    {

        $ip = $_SERVER['REMOTE_ADDR'];
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
            foreach ($matches[0] AS $xip) {
                if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
                    $ip = $xip;
                    break;
                }
            }
        } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CF_CONNECTING_IP'])) {
            $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
        } elseif (isset($_SERVER['HTTP_X_REAL_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_X_REAL_IP'])) {
            $ip = $_SERVER['HTTP_X_REAL_IP'];
        }
        return $ip;
    }

    //写入操作日志
    protected function logData($action='',$admin_id=0){
        $pt['time'] = time();
        $pt['admin_id'] =  $admin_id==0?$this->adminData['id']:$admin_id;
        $pt['action'] =  trim($action);
        Db::table('tb_admin_log')->insert($pt);
    }

    //分类列表
    public function type_list_data($page_type=1){
        $listData = Db::table('an_type')
        ->where('type', $page_type)
        ->order('id', 'desc')
        ->select()
        ->toArray(); 
        $this->assign('listData',json_encode($listData));
        return $this->fetch();
    }

    //添加分类
    public function type_add_data(){
        $pt = input("post.");
        $pt['create_time'] = time();
        $pt['up_time'] = time();
        $pt['name'] =  trim($pt['name']);
        $pt['type'] =  trim($pt['type']);
        $bool = Db::table('an_type')->insert($pt);
        $this->logData('添加分类：'.$pt['name']);
        $this->info = ['code'=>1,'msg'=>'成功'];
        return json($this->info);
    }

    //编辑分类
    public function type_edit_data(){
        $pt = input("post.");
        $bool = Db::table('an_type')->where('id', $pt['id'])->data($pt)->update();
        $this->logData('编辑分类'.$pt['id']);
        $this->info = ['code'=>1,'msg'=>'成功'];
        return json($this->info);
    }

    //删除分类
    public function type_del_data(){
        $pt = input("post.");
        $bool = Db::table('an_type')->where('id',$pt['id'])->delete();
        $this->logData('删除分类'.$pt['id']);
        $this->info = ['code'=>1,'msg'=>'成功'];
        return json($this->info);
    }
    

}
